﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using System.Text;
namespace Golf.WebUI.GolfCart
{
    public partial class CartEdit : System.Web.UI.Page
    {
        public string ShopCartId
        {
            get
            {
                try
                {
                    if (Request.QueryString["cartid"] != null && Request.QueryString["cartid"].ToString() != "")
                    {

                        return Request.QueryString["cartid"].ToString().Trim();
                    }
                    else
                    {
                        return null;
                    }
                }
                catch
                {
                    return null;
                }

            }

        }
        public int ProductsNum
        {
            get
            {
                if (Request.QueryString["count"] != null && Request.QueryString["count"].ToString() != "")
                {

                    return Convert.ToInt32(Request.QueryString["count"].ToString().Trim());
                }
                else
                {
                    return 0;
                }
            }
        }
        public string[] nums = null, pros = null;
        /// <summary>
        /// 属性
        /// </summary>
        public string[] _attr = null;
        public DataTable dt = new DataTable();
        Golf.BLL.Golf_Product bllproduct = new Golf.BLL.Golf_Product();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (ShopCartId != "" && ProductsNum > 0)
            {
                Response.Buffer = true;//清理缓存
                Response.ExpiresAbsolute = DateTime.Now.AddHours(-1);
                Response.Expires = 0;
                Response.CacheControl = "no-cache";
                //修改商品数量
                Response.Write(ProductsNumChange());
            }
        }

        /// <summary>
        /// 更改购物车里产品数量
        /// </summary>
        private string ProductsNumChange()
        {
            if (Request.Cookies["GOLF"] != null)
            {
                HttpCookie ShopCart = Request.Cookies["GOLF"];
                if (ShopCart[ShopCartId.ToString()] != null && ShopCart[ShopCartId.ToString()].Trim() != "")
                {
                    //取出cookie中的值 修改并赋值之
                    string[] Products = ShopCart[ShopCartId.ToString()].Split('|');
                    ShopCart[ShopCartId.ToString()] = Products[0] + "|" + ProductsNum.ToString() + "|" + Products[2];
                    ShopCart.Expires = DateTime.Now.AddDays(30);
                    Response.Cookies.Add(ShopCart);
                }
                string _str1 = "", _str2 = "";
                int CookieCount = ShopCart.Values.Count;
                for (int i = 0; i < CookieCount; i++)
                {
                    if (ShopCart.Values[i] == null || ShopCart.Values[i].Trim() == "")
                    {
                        continue;
                    }
                    string[] Products = ShopCart.Values[i].Split('|');
                    _str1 += Products[0] + ",";
                    _str2 += Products[1] + ",";
                }
                if (_str1.Length > 1 & _str2.Length > 1)
                {
                    if (_str1.Substring(_str1.Length - 1, 1) == ",")
                        _str1 = _str1.Substring(0, _str1.Length - 1);
                    if (_str2.Substring(_str2.Length - 1, 1) == ",")
                        _str2 = _str2.Substring(0, _str2.Length - 1);

                    pros = _str1.Split(new char[] { ',' });//商品id
                    nums = _str2.Split(new char[] { ',' });//数量


                    dt = bllproduct.GetList("Id in(" + _str1 + ")").Tables[0];
                    //dSum3当前商品金额  dSum1总金额
                    decimal dSum1 = 0.00M, dSum3 = 0.00M, price1 = 0.00M;
                    int pnum = 0;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        price1 = decimal.Parse(dt.Rows[i]["Pro_Price"].ToString());//价格
                        DataRow dr = dt.Rows[i];
                        //循环匹配 商品  防止属性相同
                        for (int j = 0; j < pros.Length; j++)
                        {
                            int num = int.Parse(nums[j]);//数量
                            //总价
                            if (pros[j] == dr["Id"].ToString())
                            {
                                dSum1 += price1 * num;
                            }
                            //当前商品
                            if (ShopCartId == ShopCart.Values.AllKeys[j])
                            {
                                dSum3 = price1 * num;
                            }
                        }
                        pnum += int.Parse(nums[i]);
                    }
                    //总金额
                    return "prochange|" + pnum.ToString() + "|" + dSum3.ToString() + "|" + dSum1.ToString() + "|";
                }
                else
                {
                    return "error";
                }
            }
            else
            {
                return "proerror";
            }
        }
    }
}
